#!/usr/bin/perl -w
use strict;
use v5.10.1;
use LWP::UserAgent;
use Mojo::DOM;
use Getopt::Std;
use URI::Escape;

sub main::HELP_MESSAGE {
	say "Usage: ......";
}
sub main::VERSION_MESSAGE {
	say "version 0.01";
}

$Getopt::Std::STANDARD_HELP_VERSION = 1;

my $word = join(' ',@ARGV);
die "No word." if !$word;
my $url = "http://dict.cn/" . uri_escape($word);
my $ua = LWP::UserAgent->new;
$ua->agent("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:21.0) Gecko/20100101 Firefox/21.0");
my $req = HTTP::Request->new(GET=>$url);
my $res = $ua->request($req);
die "Found error in response." if (!$res->is_success);

my $dom = Mojo::DOM->new($res->content);

say "$word基本释义：";
my $basic = $dom->at('div.layout.basic ul')->children('li');
$basic->each(sub {say $_->all_text;});

say "\n例句：";
my $sort_div = $dom->at('div.layout.sort');
die "There's no examples." if ! $sort_div;
my $examples = $sort_div->children();
$examples->each(sub {
		  return if $_->attrs('class') eq "more";
		  my $node = $_;
		  given($_->type){
		    when('div'){ say $node->all_text;}
		    when('ol'){
		      $node->children->each(sub{
					      my $e = shift;
					      say $e->all_text;
					    });
		    }
		  }

});

